<?php

namespace app\admin\model\miniform;

use think\Exception;
use think\Model;


class Staff extends Model
{


    // 表名
    protected $name = 'miniform_staff';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];


    public static function init()
    {
        self::beforeInsert(function ($row) {
            $user_id = $row['user_id'] ?? 0;
            if (!$user_id) {
                throw new Exception("会员ID不能为空");
            }
            $staff = Staff::where('user_id', $user_id)->find();
            if ($staff) {
                throw new Exception("已经存在该关联会员");
            }
        });
        self::beforeWrite(function ($row) {
            $projectdata = array_filter(explode(',', $row['projectdata']));
            if (!$projectdata) {
                throw new Exception("核销项目不能为空");
            }
            if (in_array('*', $projectdata) && count($projectdata) > 1) {
                throw new Exception("全部项目不能和其它项目同时勾选");
            }
        });
    }


}
